package de.baumann.browser.Activity;


import android.support.test.espresso.ViewInteraction;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.ArrayList;
import java.util.List;

import de.baumann.browser.R;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class HomeActivityTest {


    @Test
    public void homeActivityTest() {
        // Added a sleep statement to match the app's execution delay.
        // The recommended way to handle such scenarios is to use Espresso idling resources:
        // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ViewInteraction relativeLayout = onView(
                allOf(withId(R.id.rlUCSearchbox),
                        childAtPosition(
                                allOf(withId(R.id.llBezierContain),
                                        childAtPosition(
                                                withId(R.id.llBezierLayout),
                                                0)),
                                1),
                        isDisplayed()));
        relativeLayout.check(matches(isDisplayed()));

        ViewInteraction textView = onView(
                allOf(withText("23 ℃"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.llBezierContain),
                                        0),
                                1),
                        isDisplayed()));
        textView.check(matches(withText("23 ℃")));
    }

    private static Matcher<View> childAtPosition(
            final Matcher<View> parentMatcher, final int position) {

        return new TypeSafeMatcher<View>() {
            @Override
            public void describeTo(Description description) {
                description.appendText("Child at position " + position + " in parent ");
                parentMatcher.describeTo(description);
            }

            @Override
            public boolean matchesSafely(View view) {
                ViewParent parent = view.getParent();
                return parent instanceof ViewGroup && parentMatcher.matches(parent)
                        && view.equals(((ViewGroup) parent).getChildAt(position));
            }
        };
    }


    @Test
    private void test(){
        //创建一个Integer集合的链表
        List<Integer> l = new ArrayList<Integer>();
        //当链表种存在15个数时结束像链表种插入数据
        while(l.size()<15){
            int i = (int)(Math.random()*15+1);
            if(!l.contains(i))
                l.add(i);
        }
        //迭代，输出链表中的元素
        //for(int j:l)
        //    System.out.println(j);
        for(int i=0;i<l.size();i++)
        {
            System.out.println(l.get(i));
        }
    }
}
